package com.lujianfei.waterpower.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lujianfei.waterpower.R;
import com.lujianfei.waterpower.ui.preview.PreviewActivity;
import com.lujianfei.waterpower.ui.water.WaterContract;
import com.lujianfei.waterpower.ui.water.WaterEntity;
import com.lujianfei.waterpower.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @date 创建时间:2020/7/30
 * @name 作者:陆键霏
 * @describe 描述:
 */
public class WaterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<WaterEntity> powerEntities = new ArrayList<>();
    private WaterContract.Presenter presenter;
    
    public WaterAdapter(WaterContract.Presenter presenter) {
        this.presenter = presenter;
    } 
    
    public void setData(List<WaterEntity> powerEntities) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyCallback(powerEntities, this.powerEntities));
        this.powerEntities.clear();
        if (powerEntities != null && !powerEntities.isEmpty()) {
            this.powerEntities.addAll(powerEntities);
        }
        diffResult.dispatchUpdatesTo(this);
    }
    
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_power_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final WaterEntity data = powerEntities.get(position);
        
        if (holder instanceof MyViewHolder) {
            MyViewHolder mMyViewHolder = (MyViewHolder)holder;
            mMyViewHolder.txt_title.setText(String.format("%s", data.getDateStr()));
            mMyViewHolder.txt_readout.setText(String.format("读数：%s 吨", data.getReadout()));
            if (null != data.getNext()) {
                // 时间跨度大于两个月的不显示
                long duration = data.getSet_date() - data.getNext().getSet_date();
                if (duration >= 60L * 24 * 60 * 60 * 1000) {
                    mMyViewHolder.txt_increment.setVisibility(View.GONE);
                    mMyViewHolder.txt_cost.setVisibility(View.GONE);
                } else {
                    double power = data.getReadout() - data.getNext().getReadout();
                    double cost = power * data.getNext().getUnit_price();
                    mMyViewHolder.txt_increment.setText(String.format("+%s 吨", power));
                    mMyViewHolder.txt_cost.setText(String.format("消费：%.1f 元", cost));
                    mMyViewHolder.txt_increment.setVisibility(View.VISIBLE);
                    mMyViewHolder.txt_cost.setVisibility(View.VISIBLE);
                }
            } else {
                mMyViewHolder.txt_increment.setVisibility(View.GONE);
                mMyViewHolder.txt_cost.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(data.getUri_photo())) {
                mMyViewHolder.iv_icon.setVisibility(View.GONE);
            } else {
                mMyViewHolder.iv_icon.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(data.getRemark())) {
                mMyViewHolder.txt_remark.setVisibility(View.GONE);
            } else {
                mMyViewHolder.txt_remark.setVisibility(View.VISIBLE);
            }
            mMyViewHolder.txt_unit_price.setText(String.format("单价：%s 元/吨", data.getUnit_price()));
            mMyViewHolder.txt_remark.setText(String.format("备注：%s", data.getRemark()));
            final ImageView iv_icon = mMyViewHolder.iv_icon;
            iv_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    jump2Preview(iv_icon.getContext(),data);
                }
            });

            mMyViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    presenter.showOperDialog(data);
                    return true;
                }
            });
        }
    }

    private void jump2Preview(Context context,WaterEntity data) {
        if (TextUtils.isEmpty(data.getUri_photo())) {
            ToastUtils.show("图片未设置");
            return;
        }
        Intent intent = new Intent(context, PreviewActivity.class);
        intent.putExtra(PreviewActivity.KEY_URI, data.getUri_photo());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return powerEntities.size();
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txt_title;
        LinearLayout layout_middle;
        TextView txt_readout;
        TextView txt_increment;
        TextView txt_unit_price;
        TextView txt_cost;
        ImageView iv_icon;
        TextView txt_remark;
        
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            txt_title = itemView.findViewById(R.id.txt_title);
            layout_middle = itemView.findViewById(R.id.layout_middle);
            txt_readout = itemView.findViewById(R.id.txt_readout);
            txt_increment = itemView.findViewById(R.id.txt_increment);
            txt_unit_price = itemView.findViewById(R.id.txt_unit_price);
            txt_cost = itemView.findViewById(R.id.txt_cost);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            txt_remark = itemView.findViewById(R.id.txt_remark);
        }
    }
    
    private class MyCallback extends DiffUtil.Callback {

        private List<WaterEntity> newlist;
        private List<WaterEntity> oldlist;
        
        public MyCallback(List<WaterEntity> newlist, List<WaterEntity> oldlist) {
            this.newlist = newlist;
            this.oldlist = oldlist;
        }
        
        @Override
        public int getOldListSize() {
           return oldlist!=null ? oldlist.size() : 0;
        }

        @Override
        public int getNewListSize() {
            return newlist!=null ? newlist.size() : 0;
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldlist.get(oldItemPosition).getId() == newlist.get(newItemPosition).getId();
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return oldlist.get(oldItemPosition).equals(newlist.get(newItemPosition));
        }
    }
}
